 package net.go176.backpack.command;
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import net.go176.backpack.config.ConfigManager;
import net.go176.backpack.core.CoreHelper;
import net.go176.backpack.core.DataManager;
import net.go176.backpack.core.MyDataHandlerGUI;
import net.go176.backpack.gui.InventoryType;
import net.go176.backpack.gui.OfflineInvHolder;
import net.go176.backpack.gui.OnlineInvHolder;






 import org.bukkit.World;
 import org.bukkit.command.Command;
 import org.bukkit.command.CommandSender;
 import org.bukkit.entity.Player;
 
 
 public class CmdOpen
   extends CommandBase
 {
   private ConfigManager cfgMan;
   private DataManager dataMan;
   private CoreHelper coreHelper;
   private Logger logger;
   
   public CmdOpen(CommandManager cmdMan) {
     super(cmdMan);
     this.cfgMan = cmdMan.p.getConfigManager();
     this.dataMan = cmdMan.p.getDataManager();
     this.coreHelper = cmdMan.p.getCoreHelper();
     this.logger = cmdMan.p.getLogger();
     this.labels.add("open");
     this.labels.add("o");
     this.usage = "<invtype> <player> [world]";
     this.description = "Open inventory of player. <invtype> can be " + CommandManager.lister(InventoryType.getLabels(), ", ", "\"");
     this.playerOnly = true;
     this.permission = "mybackpack.command.open";
   }
 
   
   public void execute(CommandSender cs, Command cmd, String label, String[] args) {
     if (args.length == 3 || args.length == 4)
     { Player player = (Player)cs;
       InventoryType invType = InventoryType.getType(args[1].toLowerCase());
       if (invType != null) {
         String targetName = args[2];
         Player target = this.cmdMan.p.getServer().getPlayer(targetName);
         if (target != null) {
           if (args.length == 3) {
             openOnlineInv(player, target, invType);
           } else {
             boolean online = false;
             String worldName = args[3];
             String logoutworld = this.cfgMan.getLogoutWorld(targetName);
             if (logoutworld != null) {
               String link1 = this.cfgMan.getWorldLink(worldName);
               String link2 = this.cfgMan.getWorldLink(logoutworld);
               if (link1.equals(link2)) {
                 online = true;
                 openOnlineInv(player, target, invType);
               } 
             } 
             if (!online) {
               openOfflineInv(player, targetName, invType, worldName);
             }
           }
         
         } else if (args.length == 3) {
           String worldName = player.getWorld().getName();
           openOfflineInv(player, targetName, invType, worldName);
         } else {
           String worldName = args[3];
           openOfflineInv(player, targetName, invType, worldName);
         } 
       } else {
         
         CommandManager.sendMsg(cs, "&cInvType should be " + CommandManager.lister(InventoryType.getLabels(), ", ", "\""));
       }  }
     else { CommandManager.sendMsg(cs, "&cUsage: /" + label + " " + this.usage); }
   
   }
   public void openOnlineInv(Player player, Player target, InventoryType invType) {
     MyDataHandlerGUI handler = this.coreHelper.getDataHandlerGUI(invType);
     if (handler != null) {
       OnlineInvHolder holder = this.dataMan.getAbsoluteStatus(target).getAbsoluteOnlineHolder(invType);
       handler.readOnlineInv(holder.inv, target);
       CommandManager.sendMsg((CommandSender)player, "&bOpening online &f" + invType.invName + "&b of &a" + target.getName());
       player.openInventory(holder.inv);
     } else {
       CommandManager.sendMsg((CommandSender)player, "&eInvType \"" + invType.label + "\" is currently unavailable");
     } 
   }
   public void openOfflineInv(Player player, String targetName, InventoryType invType, String worldName) {
     MyDataHandlerGUI handler = this.coreHelper.getDataHandlerGUI(invType);
     if (handler != null) {
       OfflineInvHolder holder = this.dataMan.getOfflineHolder(targetName, worldName, invType);
       boolean post = false;
       if (holder == null) {
         holder = new OfflineInvHolder(invType, targetName, worldName);
         post = true;
       } 
       String link = this.cfgMan.getWorldLink(worldName);
       try {
         handler.loadOfflineInv(holder.inv, handler.getGUIDataFile(link, targetName, false));
         if (post)
           this.dataMan.postOfflineHolder(holder); 
         CommandManager.sendMsg((CommandSender)player, "&bOpening offline &f" + invType.invName + "&b of &a" + targetName + "&b. World link: &e" + link);
         player.openInventory(holder.getInventory());
       } catch (FileNotFoundException e) {
         CommandManager.sendMsg((CommandSender)player, "&e" + invType.invName + " of " + targetName + " not found in " + worldName);
       } catch (IOException e) {
         this.logger.log(Level.WARNING, "Error loading player inventory", e);
         CommandManager.sendMsg((CommandSender)player, "&cError loading player inventory");
       } 
     } else {
       CommandManager.sendMsg((CommandSender)player, "&eInvType \"" + invType.label + "\" is currently unavailable");
     } 
   }
   
   protected List<String> tabComplete(CommandSender cs, Command cmd, String label, String[] args) {
     List<String> worldNames, cmdNames = new ArrayList<>(0);
     switch (args.length) {
       case 2:
         cmdNames = CommandManager.getCompletions(args[1], InventoryType.getLabels());
         break;
       case 3:
         cmdNames = CommandManager.getCompletions(args[2], this.cmdMan.p.getConfigManager().getLogoutWorlds());
         break;
       case 4:
         worldNames = new LinkedList<>();
         for (World world : this.cmdMan.p.getServer().getWorlds())
           worldNames.add(world.getName()); 
         cmdNames = CommandManager.getCompletions(args[3], worldNames);
         Collections.sort(cmdNames);
         break;
     } 
     return cmdNames;
   }
 }


/* Location:              D:\User\Xuan\Downloads\MyBackpack1.2.5.jar!\net\saralab\mybackpack\command\CmdOpen.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.1.3
 */